package com.ljm.sys.org.controller;

import com.ljm.common.dto.BaseResult;
import com.ljm.common.util.ResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import com.ljm.sys.org.api.SysOrgApi;
import com.ljm.sys.org.dto.SysOrgDto;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 机构 前端控制器
 * </p>
 *
 * @author HuamingChen
 * @since 2020-10-20
 */
@Api(tags = "机构")
@Controller
@RequestMapping("/org/sysOrg")
public class SysOrgController {

    /**
    * 机构接口
    */
    @Resource
    private SysOrgApi iSysOrgApi;
    @Resource
    @Lazy
    private PasswordEncoder passwordEncoder;

    @ApiOperation(value="保存机构")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @ResponseBody
    public BaseResult save(@ApiParam(value="机构") @RequestBody @Validated SysOrgDto dto){
        if(dto.getUserInfo().getId()==null){
            dto.getUserInfo().setLoginPwd(passwordEncoder.encode(dto.getUserInfo().getLoginPwd()));
        }else{
            if(StringUtils.isNotEmpty(dto.getUserInfo().getLoginPwd())){
                dto.getUserInfo().setLoginPwd(passwordEncoder.encode(dto.getUserInfo().getLoginPwd()));
            }
        }
        iSysOrgApi.saveData(dto);
        return ResultUtil.success();
    }

    @ApiOperation(value="删除机构")
    @RequestMapping(value = "/del",method = RequestMethod.DELETE)
    @ResponseBody
    public BaseResult del(@ApiParam(value="主键") @RequestParam Long id){
        iSysOrgApi.deleteData(id);
        return ResultUtil.success();
    }

    @ApiOperation(value="获取机构")
    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    @ResponseBody
    public BaseResult getById(@ApiParam(value="主键") @RequestParam Long id){
        return ResultUtil.successData(iSysOrgApi.getDataById(id));
    }

    @ApiOperation(value="分页获取机构")
    @RequestMapping(value = "/getByPage",method = RequestMethod.GET)
    @ResponseBody
    public BaseResult getByPage(@ApiParam(value="机构id") @RequestParam(required = false) String orgId,
                                @ApiParam(value="机构名称") @RequestParam(required = false) String orgName,
                                @ApiParam(value="组织代码") @RequestParam(required = false) String orgCode,
                                @ApiParam(value="页码") @RequestParam(defaultValue = "1") Integer pageNo,
                                @ApiParam(value="页大小") @RequestParam(defaultValue = "10") Integer pageSize){
        return ResultUtil.successData(iSysOrgApi.getDataByPage(orgId,orgName,orgCode,pageNo,pageSize));
    }
}
